package com.viyu.alianlk.icons.activity;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.domob.android.ads.DomobAdView;

import com.viyu.alianlk.icons.R;

/**
 * 
 * @author Viyu_Lu
 * 
 */
public class ResultBoardActivity extends Activity implements OnClickListener {

	public static final int MSG_CODE_WIN = 0;
	public static final int MSG_CODE_LOSE = 1;

	public static final String KEY_INTENT_MSGCODE = "MsgCode";
	public static final String KEY_INTENT_TIMECOST = "TimeCost";

	private Animation scaleAnimation = null;
	private MediaPlayer mediaPlayer = null;

	private TextView resultText = null;
	private TextView messageText = null;
	private Button replayButton = null;
	private Button rechooseButton = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//
		setContentView(R.layout.activity_resultboard);
		//
		resultText = (TextView) findViewById(R.id.resultboard_resulttext);
		messageText = (TextView) findViewById(R.id.resultboard_message);
		replayButton = (Button) findViewById(R.id.resultboard_replay);
		rechooseButton = (Button) findViewById(R.id.resultboard_repchoose);
		replayButton.setOnClickListener(this);
		rechooseButton.setOnClickListener(this);
		//
		Intent intent = getIntent();
		int msgCode = intent.getIntExtra(KEY_INTENT_MSGCODE, MSG_CODE_LOSE);
		int time = intent.getIntExtra(KEY_INTENT_TIMECOST, 100);
		//
		if (msgCode == MSG_CODE_WIN) {
			resultText.setText(getString(R.string.text_winner));
		} else if (msgCode == MSG_CODE_LOSE) {
			resultText.setText(getString(R.string.text_loser));
		}

		messageText.setText(getString(R.string.text_timecosted, time));

		//
		scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
		//
		// ad
		RelativeLayout adContainer = (RelativeLayout) findViewById(R.id.resultboard_adcontainer);
		DomobAdView adView = new DomobAdView(this, getString(R.string.ads_publishid), getString(R.string.ads_inlineid), DomobAdView.INLINE_SIZE_FLEXIBLE);
		adContainer.addView(adView);
		//
		mediaPlayer = MediaPlayer.create(this, R.raw.bg);
		mediaPlayer.setLooping(true);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mediaPlayer.pause();
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mediaPlayer.release();
	}

	@Override
	protected void onResume() {
		super.onResume();
		//
		mediaPlayer.start();
		resultText.startAnimation(scaleAnimation);
		messageText.startAnimation(scaleAnimation);
		replayButton.startAnimation(scaleAnimation);
		rechooseButton.startAnimation(scaleAnimation);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.resultboard_replay: {
			Intent intent = new Intent(this, PlayBoardActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
			finish();
			break;
		}
		case R.id.resultboard_repchoose: {
			finish();
			break;
		}
		}
	}
}